<script setup lang="ts">
import { computed, ref } from 'vue'
import Swiper from '@/components/Swiper.vue'
import { getBannerAPI } from '@/services/home'
import type { BannerItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { getCategoryTopAPI } from '@/services/category'
import type { CategoryTopItem, CategoryChildItem } from '@/types/category'
// 轮播图
const bannerList = ref<BannerItem[]>([])
const getBannerData = async () => {
  let res = await getBannerAPI(2)
  bannerList.value = res.result
}
// 分类列表数据
const categoryList = ref<CategoryTopItem[]>([])
// 获取分类列表数据
const getCategoryListData = async () => {
  let res = await getCategoryTopAPI()
  categoryList.value = res.result
}
const activeIndex = ref(0)
// 二级分类列表数据
const subCategoryList = computed(() => {
  return categoryList.value[activeIndex.value]?.children || []
})
// 数据是否加载完成
import PageSkeleton from './components/PageSkeleton.vue'
const isFinish = ref(false)
const onSearch = () => {}
const onCancel = () => {}
onLoad(async () => {
  await Promise.all([getCategoryListData(), getBannerData()])
  isFinish.value = true
})
</script>
<template>
  <view class="category" v-if="isFinish">
    <!-- 搜索框 -->
    <navigator
      url="/pages/search/search"
      open-type="navigate"
      hover-class="navigator-hover"
      class="search"
    >
      <text class="icon-search">请输入商品名</text>
    </navigator>
    <!-- 分类 -->
    <view class="categories">
      <scroll-view scroll-y class="primary">
        <!-- 左侧：一级分类 -->
        <view>
          <view
            class="item"
            v-for="(item, index) in categoryList"
            :key="item.id"
            :class="{ active: index === activeIndex }"
            @tap="activeIndex = index"
          >
            <text class="name">{{ item.name }}</text>
          </view>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view scroll-y class="secondary">
        <view>
          <Swiper :list="bannerList" class="banner" />
          <view class="panel" v-for="item in subCategoryList" :key="item.id">
            <view class="title">
              <text class="name">{{ item.name }}</text>
              <navigator class="more" hover-class="none">全部</navigator>
            </view>
            <view class="section">
              <navigator
                open-type="navigate"
                class="goods"
                hover-class="navigator-hover"
                v-for="goods in item.goods"
                :key="goods.id"
                :url="`/pages/goods/goods?id=${goods.id}`"
              >
                <image :src="goods.picture" mode="scaleToFill" class="image" />
                <view class="name ellipsis">{{ goods.name }}</view>
                <view class="price">
                  <text class="symbol">¥</text>
                  <text class="number">{{ goods.price }}</text>
                </view>
              </navigator>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  <PageSkeleton v-else />
</template>
<style lang="scss">
.category {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background-color: #f6f6f6;

  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10rpx 0 26rpx;
    height: 64rpx;
    margin: 16rpx 20rpx;
    color: #595c63;
    font-size: 26rpx;
    background-color: rgba(255, 255, 255, 0.5);
  }

  .icon-search {
    &::before {
      margin-right: 10rpx;
    }
  }

  /* 分类 */
  .categories {
    min-height: 400rpx;
    display: flex;
    height: calc(100vh - 56rpx);

    /* 一级分类 */
    .primary {
      width: 180rpx;
      height: 100%;
      flex: none;
      background-color: #f6f6f6;
      border-radius: 10rpx;

      .item {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 96rpx;
        font-size: 26rpx;
        color: #595c63;
        position: relative;

        &::after {
          content: '';
          position: absolute;
          left: 42rpx;
          bottom: 0;
          width: 96rpx;
          border-top: 1rpx solid #e3e4e7;
        }
      }

      .active {
        background-color: #fff;

        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          width: 8rpx;
          height: 100%;
          background-color: #27ba9b;
        }
      }
    }

    .primary .item:last-child::after,
    .primary .active::after {
      display: none;
    }

    /* 二级分类 */
    .secondary {
      height: 100%;
      background-color: #fff;
      flex: 1;

      // .banner {
      //   height: 280rpx;
      //   margin: 0 30rpx 20rpx;
      //   border-radius: 4rpx;
      //   overflow: hidden;
      // }

      .panel {
        margin: 0 30rpx 0rpx;

        .title {
          height: 60rpx;
          line-height: 60rpx;
          color: #333;
          font-size: 28rpx;
          border-bottom: 1rpx solid #f7f7f8;

          .more {
            float: right;
            padding-left: 20rpx;
            font-size: 24rpx;
            color: #999;

            &::after {
              font-family: 'erabbit' !important;
              content: '\e6c2';
            }
          }
        }

        .section {
          width: 100%;
          display: flex;
          flex-wrap: wrap;
          padding: 20rpx 0;

          .goods {
            width: 150rpx;
            margin: 0rpx 20rpx 20rpx 0;

            &:nth-child(3n) {
              margin-right: 0;
            }

            .image {
              width: 150rpx;
              height: 150rpx;
            }

            .name {
              padding: 5rpx;
              font-size: 22rpx;
              color: #333;
            }

            .price {
              padding: 5rpx;
              font-size: 18rpx;
              color: #cf4444;
            }

            .number {
              font-size: 24rpx;
              margin-left: 2rpx;
            }
          }
        }
      }
    }
  }
}
</style>
